<?php

class AppointmentController extends Controller
{
	public $layout="//layouts/nolayout";
	public function actionIndex()
	{
		$this->render('index');
	}
	
	public function actionShowAppointmentPage()
	{
		$this->render('showAppointmentPage');
	}
	
	public function actionShowVirtualRoomAppointmentPage()
	{
		$this->render('showVirtualRoomAppointmentPage');
	}
	
	//添加新的预约
	public function actionAddNewAppointment($tid,$wid,$rid,$period)
	{
		//查询假期
		$start_date=Week::model()->findByPk($wid)->start_date;
		$periodList=Period::model()->findAll();
		$day=intval($period/(count($periodList)));
		$time=strtotime('+'.$day.' day '.$start_date);
		$date=date('Y-m-d',$time);
		$hasHoliday=Holiday::model()->find("`start_date` <= '".$date."' AND `end_date` >= '".$date."'");
		if (!empty($hasHoliday))
		{
			$json=array(  
						'success'=>false,
						'res'=>'在此有假期！',
					);   
			echo CJSON::encode($json);
			return;
		}
		
		$newAppointment=new Appointment;
		$newAppointment->user_id=Yii::app()->user->id;
		$newAppointment->term_id=$tid;
		$newAppointment->room_id=$rid;
		$newAppointment->week_id=$wid;
		$periodList=Period::model()->findAll();
		$day=intval($period/(count($periodList)));
		$newAppointment->day_of_week=intval($day);
		$pindex=$period%(count($periodList));
		$newAppointment->period_id=$periodList[$pindex]->period_id;
		$res=Appointment::model()->findByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											'user_id'=>Yii::app()->user->id,
											));
		
		if (!empty($res))
		{
			$json=array(
							'success'=>false,
							'res'=>'已经在此预约了',
						);   
			echo CJSON::encode($json);
		}else{
			$maxCount=Appointment::model()->findAllByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											));
			$room_max=Room::model()->findByPK($rid);
			if (count($maxCount)>=$room_max->num_of_computer)
			{
				$json=array(
								'success'=>false,
								'res'=>'人数已满',
							);   
				echo CJSON::encode($json);
				return;
			}
			if ($newAppointment->save()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>'预约成功',
						);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
							'success'=>false,
							'res'=>'预约失败！'
						);   
				echo CJSON::encode($json);
			}
		}
	}
	
	//检查预约权限
	public function actionHasAppointmentAccess($tid,$wid,$rid,$period)
	{
		$dayName=array('一','二','三','四','五','六','日');
		$periodList=Period::model()->findAll();
		$day=intval($period/(count($periodList)));
		$pindex=$period%(count($periodList));
		//查询假期
		$start_date=Week::model()->findByPk($wid)->start_date;
		$periodList=Period::model()->findAll();
		$time=strtotime('+'.$day.' day '.$start_date);
		$date=date('Y-m-d',$time);
		$hasHoliday=Holiday::model()->find("`start_date` <= '".$date."' AND `end_date` >= '".$date."'");
		if (!empty($hasHoliday))
		{
			$json=array(  
						'success'=>false,
						'res'=>'当前为假期时段！'
					);   
			echo CJSON::encode($json);
			return;
		}
		$hasSchedule=Schedule::model()->findByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											));
		//当前已有课程安排
		if (!empty($hasSchedule))
		{
			$json=array(  
							'success'=>false,
							'title'=>'无法预约',
							'res'=>'当前已有课程',
						);
			echo CJSON::encode($json);
			return;
		}
		
		$isOpened=Open::model()->findByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											));
		//实验室未开放
		if (empty($isOpened))
		{
			$json=array(  
							'success'=>false,
							'title'=>'无法预约',
							'res'=>'当前实验室未开放',
						);
			echo CJSON::encode($json);
			return;
		}		
		
		//当前是否已经预约了
		$res=Appointment::model()->findByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											'user_id'=>Yii::app()->user->id,
											));

		if (empty($res))
		{
			$term=Term::model()->findByPK($tid);
			$week=Week::model()->findByPK($wid);
			$room=Room::model()->findByPK($rid);
		
			$json=array(  
						'success'=>true,
						'title'=>'未预约',
						'res'=>'<h2 style="color:green">点击确定以预约当前实验室</h2><p>时间：'.$term->name.' '.$week->week_describe.'</p>'.'<p>日期：['.$date.'] 周'.$dayName[intval($day)].'</p><p>实验室：'.$room->name.'</p><p>节次：'.$periodList[$pindex]->period_info.'</p>',
					);
		}else{
				$json=array(  
							'success'=>true,
							'title'=>'已预约',
							'res'=>'<h2 style="color:blue">已成功预约</h2><p>时间：'.$res->term->name.' '.$res->week->week_describe.'</p>'.'<p>日期：['.$date.'] 周'.$dayName[intval($day)].'</p><p>实验室：'.$res->room->name.'</p><p>节次：'.$periodList[$pindex]->period_info.'</p>',
						);
		}
		echo CJSON::encode($json);
	}
	
	//取消预约
	public function actionDeleteAppointment($tid,$wid,$rid,$period) 
	{
		$periodList=Period::model()->findAll();
		$day=$period/(count($periodList));
		$pindex=$period%(count($periodList));
		$res=Appointment::model()->findByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											'user_id'=>Yii::app()->user->id
											));
		if (!empty($res))
		{
			if ($res->delete()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>'删除成功',
						);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
						'success'=>false,
						'res'=>'无法删除！'
					);   
				echo CJSON::encode($json);
			}
		}else{
			$json=array(  
						'success'=>false,
						'res'=>'你没有在此预约！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	//获得预约表格
	public function actionGetAppointmentGrid($term_id,$week_id)
	{
		$dayName=array('一','二','三','四','五','六','日');
		$modelRoomList=Room::model()->findAll(array('order'=>'name ASC'));
		//填充fields
		$fields=array();
		$elem=array();
		$elem['name']='dayOfWeek';
		$elem['type']='string';
		array_push($fields,$elem);
		
		$elem['name']='period';
		$elem['type']='string';
		array_push($fields,$elem);
		
		for($i=0;$i<count($modelRoomList);$i++)
		{
			$elem['name']=$modelRoomList[$i]->room_id;
			$elem['type']='string';
			array_push($fields,$elem);
		}
		//生成即将成为json的数组
		$json = array();
		$json['fields'] = $fields;
		//填充data
		$datas=array();
		$periodList=Period::model()->findAll();
		$nowAppointment=new Appointment;
		$elem=array();
		
		for ($dayi=0;$dayi<7;$dayi++)
		{
			//查询假期
			$start_date=Week::model()->findByPk($week_id)->start_date;
			$time=strtotime('+'.$dayi.' day '.$start_date);
			$date=date('Y-m-d',$time);
			$hasHoliday=Holiday::model()->find("`start_date` <= '".$date."' AND `end_date` >= '".$date."'");
			//如果有假期，则在这一天显示
			if (!empty($hasHoliday))
			{
				$elem['dayOfWeek']='周'.($dayName[$dayi]).'<p style="color:red">'.$hasHoliday->name.'</p>';
				for ($periodi=0;$periodi<count($periodList);$periodi++)
				{
					$elem['period']=$periodList[$periodi]->period_info;
					for($roomi=0;$roomi<count($modelRoomList);$roomi++)
						$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%;" data-qtitle="不可预约" data-qwidth="200" data-qtip="'.$hasHoliday->name.'"><p style="color:red">-</p></div>';
					array_push($datas,$elem);
					$elem=array();
					$elem['dayOfWeek']='';
				}
				continue;
			}
			else
			{
				$elem['dayOfWeek']='周'.($dayName[$dayi]);
			}
			
			for ($periodi=0;$periodi<count($periodList);$periodi++)
			{
				$elem['period']=$periodList[$periodi]->period_info;
				for($roomi=0;$roomi<count($modelRoomList);$roomi++)
				{
					$nowSchedule=Schedule::model()->findByAttributes(array(
											'term_id'=>$term_id,	
											'week_id'=>$week_id,
											'day_of_week'=>$dayi,
											'period_id'=>$periodList[$periodi]->period_id,
											'room_id'=>$modelRoomList[$roomi]->room_id,
											));
					//当前已有课程安排
					if (!empty($nowSchedule))
					{
						//使用关系查询
						$text=$nowSchedule->user->name.'<br>'.$nowSchedule->course->name;
						$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%;" data-qtitle="已排课【无法预约】" data-qwidth="200" data-qtip="['.$date.']<br>周'.($dayName[$dayi]).$periodList[$periodi]->period_info.'<br>'.$modelRoomList[$roomi]->name.'">'.$text.'</div>';
					}else{
						$nowAppointment=Appointment::model()->findByAttributes(array(
											'term_id'=>$term_id,	
											'week_id'=>$week_id,
											'day_of_week'=>$dayi,
											'period_id'=>$periodList[$periodi]->period_id,
											'room_id'=>$modelRoomList[$roomi]->room_id,
											'user_id'=>Yii::app()->user->id,
											));
						if (!empty($nowAppointment))
						{
							//使用关系查询
							$text='<p style="color:blue">已成功预约</p>';
							$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%" data-qtitle="已预约（可修改）" data-qwidth="200" data-qtip="['.$date.']<br>周'.($dayName[$dayi]).$periodList[$periodi]->period_info.'<br>'.$modelRoomList[$roomi]->name.'">'.$text.'</div>';
						}else{
							$isOpened=Open::model()->findByAttributes(array(
											'term_id'=>$term_id,	
											'week_id'=>$week_id,
											'day_of_week'=>$dayi,
											'period_id'=>$periodList[$periodi]->period_id,
											'room_id'=>$modelRoomList[$roomi]->room_id,
											));
							if (empty($isOpened))
							{
								$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%" data-qtitle="实验室未开放" data-qwidth="100" data-qtip="['.$date.']<br>周'.($dayName[$dayi]).$periodList[$periodi]->period_info.'<br>'.$modelRoomList[$roomi]->name.'"><p style="color:red">x</p></div>';
							}else{
								$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%" data-qtitle="可预约" data-qwidth="100" data-qtip="['.$date.']<br>周'.($dayName[$dayi]).$periodList[$periodi]->period_info.'<br>'.$modelRoomList[$roomi]->name.'"><p style="color:green">√</p></div>';
							}
						}
					}
				}
				array_push($datas,$elem);
				$elem=array();
				$elem['dayOfWeek']='';
			}
		}
		
		$json['data'] = $datas;
		//填充columns
		$columns=array();			
		$elem=array();
		$weekName=Week::model()->findByPk($week_id);
		$elem['header']=$weekName['week_describe'];
		$elem['dataIndex']='dayOfWeek';
		$elem['sortable']=false;
		$elem['width']='10%';
		array_push($columns,$elem);
		
		$elem['header']='节次';
		$elem['dataIndex']='period';
		$elem['sortable']=false;
		$elem['width']='10%';
		array_push($columns,$elem);
		
		for($i=0;$i<count($modelRoomList);$i++)
		{
			$elem['header']=$modelRoomList[$i]->name.'('.$modelRoomList[$i]->num_of_computer.')';
			$elem['dataIndex']=$modelRoomList[$i]->room_id;
			$elem['sortable']=false;
			$elem['width']=intval(80/count($modelRoomList)).'%';
			array_push($columns,$elem);
		}
		$json['columns'] = $columns;
		
		echo CJSON::encode($json);
	}
	
	public function actionGetVirtualRoomAppointmentGrid($term_id,$week_id,$course_id)
	{
		$dayName=array('一','二','三','四','五','六','日');
		$modelRoomList=Course_Room::model()->findAllByAttributes(array('course_id'=>$course_id));
		for($i=0;$i<count($modelRoomList);$i++)
		{
			$modelRoomList[$i]=$modelRoomList[$i]->room;
		}
		//填充fields
		$fields=array();
		$elem=array();
		$elem['name']='dayOfWeek';
		$elem['type']='string';
		array_push($fields,$elem);
		
		$elem['name']='period';
		$elem['type']='string';
		array_push($fields,$elem);
		
		for($i=0;$i<count($modelRoomList);$i++)
		{
			$elem['name']=$modelRoomList[$i]->room_id;
			$elem['type']='string';
			array_push($fields,$elem);
		}
		//生成即将成为json的数组
		$json = array();
		$json['fields'] = $fields;
		//填充data
		$datas=array();
		$periodList=Period::model()->findAll();
		$nowAppointment=new Appointment;
		$elem=array();
		
		for ($dayi=0;$dayi<7;$dayi++)
		{
			//查询假期
			$start_date=Week::model()->findByPk($week_id)->start_date;
			$time=strtotime('+'.$dayi.' day '.$start_date);
			$date=date('Y-m-d',$time);
			$hasHoliday=Holiday::model()->find("`start_date` <= '".$date."' AND `end_date` >= '".$date."'");
			//如果有假期，则在这一天显示
			if (!empty($hasHoliday))
			{
				$elem['dayOfWeek']='周'.($dayName[$dayi]).'<p style="color:red">'.$hasHoliday->name.'</p>';
				for ($periodi=0;$periodi<count($periodList);$periodi++)
				{
					$elem['period']=$periodList[$periodi]->period_info;
					for($roomi=0;$roomi<count($modelRoomList);$roomi++)
						$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%" data-qtitle="不可预约" data-qwidth="200" data-qtip="'.$hasHoliday->name.'"><p style="color:red">-</p></div>';
					array_push($datas,$elem);
					$elem=array();
					$elem['dayOfWeek']='';
				}
				continue;
			}
			else
			{
				$elem['dayOfWeek']='周'.($dayName[$dayi]);
			}
			
			for ($periodi=0;$periodi<count($periodList);$periodi++)
			{
				$elem['period']=$periodList[$periodi]->period_info;
				for($roomi=0;$roomi<count($modelRoomList);$roomi++)
				{
					$nowSchedule=Schedule::model()->findByAttributes(array(
											'term_id'=>$term_id,	
											'week_id'=>$week_id,
											'day_of_week'=>$dayi,
											'period_id'=>$periodList[$periodi]->period_id,
											'room_id'=>$modelRoomList[$roomi]->room_id,
											));
					//当前已有课程安排
					if (!empty($nowSchedule))
					{
						//使用关系查询
						$text=$nowSchedule->user->name.'<br>'.$nowSchedule->course->name;
						$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%" data-qtitle="已排课【无法预约】" data-qwidth="200" data-qtip="['.$date.']<br>周'.($dayName[$dayi]).$periodList[$periodi]->period_info.'<br>'.$modelRoomList[$roomi]->name.'">'.$text.'</div>';
					}else{
						$nowAppointment=Appointment::model()->findByAttributes(array(
											'term_id'=>$term_id,	
											'week_id'=>$week_id,
											'day_of_week'=>$dayi,
											'period_id'=>$periodList[$periodi]->period_id,
											'room_id'=>$modelRoomList[$roomi]->room_id,
											'user_id'=>Yii::app()->user->id,
											));
						if (!empty($nowAppointment))
						{
							//使用关系查询
							$text='<p style="color:blue">已成功预约</p>';
							$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%" data-qtitle="已预约" data-qwidth="200" data-qtip="['.$date.']<br>周'.($dayName[$dayi]).$periodList[$periodi]->period_info.'<br>'.$modelRoomList[$roomi]->name.'">'.$text.'</div>';
						}else{
							$isOpened=Open::model()->findByAttributes(array(
											'term_id'=>$term_id,	
											'week_id'=>$week_id,
											'day_of_week'=>$dayi,
											'period_id'=>$periodList[$periodi]->period_id,
											'room_id'=>$modelRoomList[$roomi]->room_id,
											));
							if (empty($isOpened))
							{
								$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%" data-qtitle="实验室未开放" data-qwidth="100" data-qtip="['.$date.']<br>周'.($dayName[$dayi]).$periodList[$periodi]->period_info.'<br>'.$modelRoomList[$roomi]->name.'"><p style="color:red">x</p></div>';
							}else{
								$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%" data-qtitle="可预约" data-qwidth="100" data-qtip="['.$date.']<br>周'.($dayName[$dayi]).$periodList[$periodi]->period_info.'<br>'.$modelRoomList[$roomi]->name.'"><p style="color:green">√</p></div>';
							}
						}
					}
				}
				array_push($datas,$elem);
				$elem=array();
				$elem['dayOfWeek']='';
			}
		}
		
		$json['data'] = $datas;
		//填充columns
		$columns=array();			
		$elem=array();
		$weekName=Week::model()->findByPk($week_id);
		$elem['header']=$weekName['week_describe'];
		$elem['dataIndex']='dayOfWeek';
		$elem['sortable']=false;
		$elem['width']='10%';
		array_push($columns,$elem);
		
		$elem['header']='节次';
		$elem['dataIndex']='period';
		$elem['sortable']=false;
		$elem['width']='10%';
		array_push($columns,$elem);
		
		for($i=0;$i<count($modelRoomList);$i++)
		{
			$elem['header']=$modelRoomList[$i]->name.'('.$modelRoomList[$i]->num_of_computer.')';
			$elem['dataIndex']=$modelRoomList[$i]->room_id;
			$elem['sortable']=false;
			$elem['width']=intval(80/count($modelRoomList)).'%';
			array_push($columns,$elem);
		}
		$json['columns'] = $columns;
		
		echo CJSON::encode($json);
	}
	
	
	public function actionShowAllAppointmentPage()
	{
		$this->render('showAllAppointmentPage');
	}
	
	//获得预约表格
	public function actionGetAllAppointmentGrid($term_id,$week_id)
	{
		$dayName=array('一','二','三','四','五','六','日');
		$modelRoomList=Room::model()->findAll(array('order'=>'name ASC'));
		//填充fields
		$fields=array();
		$elem=array();
		$elem['name']='dayOfWeek';
		$elem['type']='string';
		array_push($fields,$elem);
		
		$elem['name']='period';
		$elem['type']='string';
		array_push($fields,$elem);
		
		for($i=0;$i<count($modelRoomList);$i++)
		{
			$elem['name']=$modelRoomList[$i]->room_id;
			$elem['type']='string';
			array_push($fields,$elem);
		}
		//生成即将成为json的数组
		$json = array();
		$json['fields'] = $fields;
		//填充data
		$datas=array();
		$periodList=Period::model()->findAll();
		$nowAppointment=new Appointment;
		$elem=array();
		
		for ($dayi=0;$dayi<7;$dayi++)
		{
			//查询假期
			$start_date=Week::model()->findByPk($week_id)->start_date;
			$time=strtotime('+'.$dayi.' day '.$start_date);
			$date=date('Y-m-d',$time);
			$hasHoliday=Holiday::model()->find("`start_date` <= '".$date."' AND `end_date` >= '".$date."'");
			//如果有假期，则在这一天显示
			if (!empty($hasHoliday))
			{
				$elem['dayOfWeek']='周'.($dayName[$dayi]).'<p style="color:red">'.$hasHoliday->name.'</p>';
				for ($periodi=0;$periodi<count($periodList);$periodi++)
				{
					$elem['period']=$periodList[$periodi]->period_info;
					for($roomi=0;$roomi<count($modelRoomList);$roomi++)
						$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%" data-qtitle="不可预约" data-qwidth="200" data-qtip="'.$hasHoliday->name.'"><p style="color:red">-</p></div>';
					array_push($datas,$elem);
					$elem=array();
					$elem['dayOfWeek']='';
				}
				continue;
			}
			else
			{
				$elem['dayOfWeek']='周'.($dayName[$dayi]);
			}
			
			for ($periodi=0;$periodi<count($periodList);$periodi++)
			{
				$elem['period']=$periodList[$periodi]->period_info;
				for($roomi=0;$roomi<count($modelRoomList);$roomi++)
				{
					$nowSchedule=Schedule::model()->findByAttributes(array(
											'term_id'=>$term_id,	
											'week_id'=>$week_id,
											'day_of_week'=>$dayi,
											'period_id'=>$periodList[$periodi]->period_id,
											'room_id'=>$modelRoomList[$roomi]->room_id,
											));
					//当前已有课程安排
					if (!empty($nowSchedule))
					{
						//使用关系查询
						$text=$nowSchedule->user->name.'<br>'.$nowSchedule->course->name;
						$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%" data-qtitle="已排课【无法预约】" data-qwidth="200" data-qtip="['.$date.']<br>周'.($dayName[$dayi]).$periodList[$periodi]->period_info.'<br>'.$modelRoomList[$roomi]->name.'">'.$text.'</div>';
					}else{
						$nowAppointment=Appointment::model()->findAllByAttributes(array(
											'term_id'=>$term_id,	
											'week_id'=>$week_id,
											'day_of_week'=>$dayi,
											'period_id'=>$periodList[$periodi]->period_id,
											'room_id'=>$modelRoomList[$roomi]->room_id,
											));
						if (!empty($nowAppointment))
						{
							//使用关系查询
							$text='<p style="color:blue">'.count($nowAppointment).'人预约</p>';
							$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%" data-qtitle="已有预约" data-qwidth="200" data-qtip="['.$date.']<br>周'.($dayName[$dayi]).$periodList[$periodi]->period_info.'<br>'.$modelRoomList[$roomi]->name.'">'.$text.'</div>';
						}else{
							$isOpened=Open::model()->findByAttributes(array(
											'term_id'=>$term_id,	
											'week_id'=>$week_id,
											'day_of_week'=>$dayi,
											'period_id'=>$periodList[$periodi]->period_id,
											'room_id'=>$modelRoomList[$roomi]->room_id,
											));
							if (empty($isOpened))
							{
								$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%" data-qtitle="实验室未开放" data-qwidth="100" data-qtip="['.$date.']<br>周'.($dayName[$dayi]).$periodList[$periodi]->period_info.'<br>'.$modelRoomList[$roomi]->name.'"><p style="color:red">x</p></div>';
							}else{
								$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%;height:100%" data-qtitle="可预约" data-qwidth="100" data-qtip="['.$date.']<br>周'.($dayName[$dayi]).$periodList[$periodi]->period_info.'<br>'.$modelRoomList[$roomi]->name.'"><p style="color:green">√</p></div>';
							}
						}
					}
				}
				array_push($datas,$elem);
				$elem=array();
				$elem['dayOfWeek']='';
			}
		}
		
		$json['data'] = $datas;
		//填充columns
		$columns=array();			
		$elem=array();
		$weekName=Week::model()->findByPk($week_id);
		$elem['header']=$weekName['week_describe'];
		$elem['dataIndex']='dayOfWeek';
		$elem['sortable']=false;
		$elem['width']='10%';
		array_push($columns,$elem);
		
		$elem['header']='节次';
		$elem['dataIndex']='period';
		$elem['sortable']=false;
		$elem['width']='10%';
		array_push($columns,$elem);
		
		for($i=0;$i<count($modelRoomList);$i++)
		{
			$elem['header']=$modelRoomList[$i]->name.'('.$modelRoomList[$i]->num_of_computer.')';
			$elem['dataIndex']=$modelRoomList[$i]->room_id;
			$elem['sortable']=false;
			$elem['width']=intval(80/count($modelRoomList)).'%';
			array_push($columns,$elem);
		}
		$json['columns'] = $columns;
		
		echo CJSON::encode($json);
	}
	
	function actionGetAllAppointment($tid,$wid,$rid,$period)
	{
		$dayName=array('一','二','三','四','五','六','日');
		$periodList=Period::model()->findAll();
		$day=intval($period/(count($periodList)));
		$pindex=$period%(count($periodList));
		//查询假期
		$start_date=Week::model()->findByPk($wid)->start_date;
		$periodList=Period::model()->findAll();
		$time=strtotime('+'.$day.' day '.$start_date);
		$date=date('Y-m-d',$time);
		$hasHoliday=Holiday::model()->find("`start_date` <= '".$date."' AND `end_date` >= '".$date."'");
		if (!empty($hasHoliday))
		{
			$json=array(  
						'success'=>false,
						'res'=>'当前为假期时段！'
					);   
			echo CJSON::encode($json);
			return;
		}
		$hasSchedule=Schedule::model()->findByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											));
		//当前已有课程安排
		if (!empty($hasSchedule))
		{
			$json=array(  
							'success'=>false,
							'title'=>'无法预约',
							'res'=>'当前已有课程',
						);
			echo CJSON::encode($json);
			return;
		}
		//当前是否已经预约了
		$res=Appointment::model()->findAllByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											));

		
		$term=Term::model()->findByPK($tid);
		$week=Week::model()->findByPK($wid);
		$room=Room::model()->findByPK($rid);
				
		if (empty($res))
		{
			$json=array(  
						'success'=>true,
						'title'=>'暂无预约',
						'res'=>'<h2 style="color:green">实验室无人预约</h2><p>时间：'.$term->name.' '.$week->week_describe.'</p>'.'<p>日期：['.$date.'] 周'.$dayName[intval($day)].'</p><p>实验室：'.$room->name.'</p><p>节次：'.$periodList[$pindex]->period_info.'</p>',
					);
		}else{
			$text='名单如下：';
			for ($i=0;$i<count($res);$i++)
				$text=$text.'<p>'.($i+1).'、'.$res[$i]->user->PID.' '.$res[$i]->user->name.'</p>';
			$json=array(  
						'success'=>true,
						'title'=>'已有'.count($res).'人预约',
						'res'=>'<h2 style="color:blue">已有'.count($res).'人成功预约</h2><p>时间：'.$term->name.' '.$week->week_describe.'</p>'.'<p>日期：['.$date.'] 周'.$dayName[intval($day)].'</p><p>实验室：'.$room->name.'</p><p>节次：'.$periodList[$pindex]->period_info.'</p>'.$text,
					);
		}
		echo CJSON::encode($json);
	}
}